package Reseau;

import Controler.Controler;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SendTCP extends Thread {
    //long tailleFichier;
    //String nomFichier;
    File file;
    ServerSocket serverSocket;
    Socket socket = null;
    int port = 2234;
    
    public SendTCP(File _file){
        System.out.println("Erreur01");
        this.file = _file;
        try {    
            socket = new Socket();
            //On se connecte au serveur TCP
            serverSocket = new ServerSocket(port);
            System.out.println("Erreur02");
        } catch (IOException ex) {
            System.out.println("Erreur: impossible de créer le socker pour SendTCP");
        }
        System.out.println("Erreur03");
    }
    
    public Socket getSocket() {
        System.out.println("Erreur04" + socket.getPort());
        return socket;
    }

    public void run(){            
        try {            
            //On attend la connexion d'un client
            socket = serverSocket.accept();
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader( socket.getInputStream()));
            int i = 0;
            String request;
            String reply;
            while (true) {
                 request = in.readLine();
                 // executer le service
                 // traiter  request pour fournir reply
                 i = i++;
                 reply = "env : " + i;
                 out.println(reply);
             }
            
           
        } catch (IOException e) {
            System.out.println("Erreur: les connexions TCP ne sont pas acceptées par le serveur");
        }
            
    }
}
